//
//  YHGameLayerLoading.cpp
//  ThunderWarriors
//
//  Created by heinic on 13-6-24.
//
//

#include "YHGameLayerLoading.h"
#include "YHGameLayerMain.h"
#include "YHGameMainScene.h"
#include "YHGameSceneManager.h"
#include "SimpleAudioEngine.h"
#include "cocos2d.h"
USING_NS_CC;

IMPLEMENT_CREATE_CLASS(YHGameLayerLoading)

YHGameLayerLoading::YHGameLayerLoading()
{

}

YHGameLayerLoading::~ YHGameLayerLoading()
{
    
}

bool YHGameLayerLoading::init()
{
    bool bRet = false;
    
    //1.get screen's size
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    CCPoint winCenter=ccp(winSize.width/2,winSize.height/2);
    progress =CCProgressTimer::create(CCSprite::create("progressbar.png"));
    progress->setPercentage(0);
    progress->setScale(0.5f);
    progress->setMidpoint(ccp(0,0.5));
    progress->setBarChangeRate(ccp(1,0));
    progress->setType(kCCProgressTimerTypeBar);
    progress->setPosition(winCenter);
    this->addChild(progress);
    
  
    CCLabelTTF * loadingText = CCLabelTTF::create("loading...","Arial" ,20);
    loadingText->setPosition(ccpAdd(winCenter, ccp(0,50)));
    this->addChild(loadingText);
                                                
        
    bRet = true;
    return bRet;
}

void YHGameLayerLoading::onEnterTransitionDidFinish()
{
    
    CCDictionary *manifest = CCDictionary::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("preloadAssetManifest.plist"));
    CCArray * spriteSheets= (CCArray *)(manifest->objectForKey("SpriteSheets"));
    CCArray * images= (CCArray *)(manifest->objectForKey("Images"));
    CCArray * soundFX= (CCArray *)(manifest->objectForKey("SoundFX"));
    CCArray * music= (CCArray *)(manifest->objectForKey("Music"));
    CCArray * assets= (CCArray *)(manifest->objectForKey("Assets"));
    assetCount = spriteSheets->count()+images->count()+soundFX->count()+music->count()+assets->count();
    progressInterval = 100.0 / (float) assetCount;
    if (soundFX) {
        CCLOG("Start loading sounds");
        CCObject* pObj;
        CCARRAY_FOREACH(soundFX,pObj)
        {
            CCString* soundClip=(CCString*)pObj;
            CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(soundClip->getCString());
            this->progressUpdate();
        }
    }
    
    if (spriteSheets) {
        CCLOG("Start loading spriteSheets");
        CCObject* pObj;
        CCARRAY_FOREACH(spriteSheets,pObj){
            CCString* spriteSheet=(CCString*)pObj;
            CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(spriteSheet->getCString());
            this->progressUpdate();
        
        }
    }
    
    if (images) {
        CCLOG("LoadingScreen - loadImages: You need to tell me what to do.");
        CCObject* pObj;
        CCARRAY_FOREACH(images,pObj){
            CCString* image=(CCString*)pObj;
            //do something with the images
            this->progressUpdate();
        }

    }
    
    if (music){
        CCLOG("Start loading music");
        CCObject* pObj;
        CCARRAY_FOREACH(music,pObj){
            CCString* musicFile=(CCString*)pObj;
            CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(musicFile->getCString());
            this->progressUpdate();
        }

    }
    
    if (assets) {
        CCLOG("LoadingScreen - loadAssets: You need to tell me what to do.");
        CCObject* pObj;
        CCARRAY_FOREACH(assets,pObj){
            CCString* asset=(CCString*)pObj;
            //do something with the images
            this->progressUpdate();
        }

    }
    
    CCLayer::onEnterTransitionDidFinish();


}

void YHGameLayerLoading::progressUpdate()
{
    if(--assetCount)
    {
        // 以备后面显示进度条用
        progress->setPercentage(100.0f-(progressInterval*assetCount) ); 
    }
    else{
        CCProgressFromTo *ac=CCProgressFromTo::create(0.5f, progress->getPercentage(), 100);
        
        CCAction *action =  CCSequence::create(ac,
                                              CCCallFunc::create(this,callfunc_selector(YHGameLayerLoading::loadComplete)),
                                              NULL);
        progress->runAction(action);
    }
}

void YHGameLayerLoading::loadComplete(){

    CCLOG("All done loading assets.");
    
    CCDelayTime * delay = CCDelayTime::create(2.0f);
    this->runAction(delay);
    
   // YHGameLayerMain * layer = (YHGameLayerMain*) YHGameSceneManager::sharedGameSceneManager()->pushLayerWithId("YHGameLayerMain", GAME_LAYER_MAIN);
    //layer->init();
    
    YHGameSceneManager::sharedGameSceneManager()->gotoSceneWithId("YHGameMainScene", GAME_SCENE_MAIN);
    YHGameLayerMain * layer = (YHGameLayerMain*) YHGameSceneManager::sharedGameSceneManager()->pushLayerWithId("YHGameLayerMain", GAME_LAYER_MAIN);
    layer->init();
   // layer->init();
    //CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.0f, YHGameLayerMain::scene));

}
